/** 
*	Group: Y1
*	Usuage: call CollisionDetect.detect(Object1-X-Coordinate, Object1-Y-Coordinate, Distance from coordinate to box bound for Object1, Object2-X-Coord, Object2-Y-Coord, Object 2 bounds);
*	Collison Detection Engine - uses rectangular bounds to detect collisions between objects
*	(Should run on server side)
* 	@author Venkat Patnala
*/
package lightspeed.Backend;
public class CollisionDetect
{
/**
* The BOUNDX is  width of the gameplay board.
*/
public static final int BOUNDX = 3000;
/**
* The BOUNDY is height of the gameplay board.
*/
public static final int BOUNDY = 3000;
/**
* Detect method detects the collision between the bullet and ship
* @param coordX1 x-coordinate of game object one
* @param coordY1 y-coordinate of game object one
* @param bound1 range to check for object two
* @param coordX2 x-coordinate of game object two
* @param coordY2 y-coordinate of game object two
* @param bound2 range to check for object two
* @return boolean that determines wheather or not there was collision
*/
	public static boolean detect(float coordX1, float coordY1, float bound1, float coordX2, float coordY2, float bound2)
	{
		boolean collisonDetected = false;
		float obj1xl, obj1xr, obj1yd, obj1yu, obj2xl, obj2xr, obj2yd, obj2yu;
		
		obj1xl = coordX1-bound1;
		obj1xr = coordX1+bound1;
		obj1yu = coordY1-bound1;
		obj1yd = coordY1+bound1;
		
		obj2xl = coordX2-bound2;
		obj2xr = coordX2+bound2;
		obj2yu = coordY2-bound2;
		obj2yd = coordY2+bound2;
		
		// Fixing bounds for object 1 for ranges outside game area
		if ((coordX1-bound1)<0)
			obj1xl = 0;
		if ((coordX1+bound1)>BOUNDX)
			obj1xr = BOUNDX;
		if ((coordY1-bound1)<0)
			obj1yu = 0;
		if ((coordY1+bound1)>BOUNDY)
			obj1yu = BOUNDY;
		
		// Fixing bounds for object 2 for ranges outside game area
		if ((coordX2-bound2)<0)
			obj2xl = 0;
		if ((coordX2+bound2)>BOUNDX)
			obj2xr = BOUNDX;
		if ((coordY2-bound2)<0)
			obj2yu = 0;
		if ((coordY2+bound2)>BOUNDY)
			obj2yu = BOUNDY;
		
		//Check X range of object 1 with X range for object 2
		
		if ((obj1xl<=obj2xl)&&(obj2xl<=obj1xr) || (obj1xl>=obj2xl)&&(obj2xr>=obj1xl))
		{ //at this point checked x conditions and they overlap
			if ((obj1yu<=obj2yu)&&(obj2yu<=obj1yd) || (obj1yu>=obj2yu)&&(obj2yd>=obj1yu))
			{ //at this point checked y conditions and they overlap
				collisonDetected = true;
			}
		} 
		
		return collisonDetected;
		
	}
}